home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / LED.PAK / LEDWIND.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.5 KB  |  273 lines

  1. //----------------------------------------------------------------------------
  2. // LED Window to Display Numbers
  3. //
  4. //  ledwind.cpp
  5. //
  6. //----------------------------------------------------------------------------
  7. #include <owl/pch.h>
  8. #include <owl/dc.h>
  9. #include <owl/gdiobjec.h>
  10. #include "ledwind.h"
  11. #include <time.h>
  12.  
  13. const int DigitW = 13;
  14. const int DigitH = 23;
  15.  
  16. //
  17. //
  18. //
  19. class TLedDigit : public TWindow {
  20.   public:
  21.     TLedDigit(TWindow* parent, TPoint& p);
  22.     void UpdateDigit(TBitmap* ledBmp);
  23.  
  24.   protected:
  25.     void Paint(TDC& dc, bool erase, TRect& rect);
  26.  
  27.     TBitmap* CurrentLedBmp;      // Digit bitmap Currently in Window
  28.  
  29. //  DECLARE_RESPONSE_TABLE(TLedDigit);
  30. };
  31.  
  32. //DEFINE_RESPONSE_TABLE1(TLedDigit, TWindow)
  33. //END_RESPONSE_TABLE;
  34.  
  35. //
  36. // LedDigit - Create a new LedDigit
  37. //
  38. TLedDigit::TLedDigit(TWindow* parent, TPoint& p)
  39. :
  40.   TWindow(parent, "")
  41. {
  42.   Attr.X = p.x;
  43.   Attr.Y = p.y;
  44.   Attr.W = DigitW;
  45.   Attr.H = DigitH;
  46.   Attr.Style = WS_VISIBLE|WS_CHILD;
  47.   CurrentLedBmp = 0;
  48. }
  49.  
  50. //
  51. // UpdateDigit - Set the digit in question invalid if it has changed
  52. //
  53. void TLedDigit::UpdateDigit(TBitmap* ledBmp)
  54. {
  55.   if (ledBmp != CurrentLedBmp) {
  56.     CurrentLedBmp = ledBmp;
  57.     Invalidate(false);      // Assumes that HWindow is valid
  58.   }
  59. }
  60.  
  61. //
  62. // Paint - Paint the LedDigit
  63. //
  64. void TLedDigit::Paint(TDC& dc, bool /*erase*/, TRect& /*rect*/)
  65. {
  66.   if (CurrentLedBmp) {
  67.     TMemoryDC  memDC(dc);
  68.     memDC.SelectObject(*CurrentLedBmp);
  69.     dc.BitBlt(0, 0, 13, 24, memDC, 0, 0, SRCCOPY);
  70.   }
  71. }
  72.  
  73. //----------------------------------------------------------------------------
  74.  
  75. DEFINE_RESPONSE_TABLE1(TLedWindow, TWindow)
  76.   EV_WM_TIMER,
  77. END_RESPONSE_TABLE;
  78.  
  79. //
  80. // Called with parent window handle, int x and y offsets in parent,
  81. // int number of digits, and bool beveled setting.
  82. //
  83. TLedWindow::TLedWindow(TWindow* parent, int x, int y,
  84.                        int digits, bool beveled, bool clock, long value)
  85. :
  86.   TWindow(parent, "")
  87. {
  88.   LedValue = value;
  89.   if (clock)
  90.   {
  91.     digits = 8;                          // A clock HH:MM:SS
  92.     ClockWork = LedValue;                // Work field for localtime
  93.     if (ClockWork < 86400l)              // Time is actually since -05:00
  94.       ClockWork = ClockWork + 18000l;    // on 0th Jan 1970 !
  95.     tmLedValue = localtime(&ClockWork);  // Point struct at Value
  96.   }
  97.  
  98.   if (digits > 10)
  99.     digits = 10;
  100.  
  101.   Attr.W = 13*digits;
  102.   Attr.H = 24;
  103.   DigitStart = Attr.W;
  104.   if (beveled)
  105.   {
  106.     Attr.W = Attr.W + 2;
  107.     Attr.H = Attr.H + 2;
  108.     DigitStart--;
  109.   }
  110.   Attr.X = x;
  111.   Attr.Y = y;
  112.   Attr.Style = WS_VISIBLE|WS_CHILD;
  113.  
  114.   HINSTANCE inst = *GetApplication();
  115.   LedBmp[0] = new TBitmap(inst, "Led0Bmp");
  116.   LedBmp[1] = new TBitmap(inst, "Led1Bmp");
  117.   LedBmp[2] = new TBitmap(inst, "Led2Bmp");
  118.   LedBmp[3] = new TBitmap(inst, "Led3Bmp");
  119.   LedBmp[4] = new TBitmap(inst, "Led4Bmp");
  120.   LedBmp[5] = new TBitmap(inst, "Led5Bmp");
  121.   LedBmp[6] = new TBitmap(inst, "Led6Bmp");
  122.   LedBmp[7] = new TBitmap(inst, "Led7Bmp");
  123.   LedBmp[8] = new TBitmap(inst, "Led8Bmp");
  124.   LedBmp[9] = new TBitmap(inst, "Led9Bmp");
  125.   LedBmp[10] = new TBitmap(inst, "LedColBmp");
  126.  
  127.   Beveled = beveled;
  128.   MaxDigits = digits;
  129.  
  130.   TimerActive = false;
  131.   TimerInterval = 0;
  132.   ClockWindow = clock;
  133.  
  134.   TPoint pos(DigitStart - (DigitW-1), 0);
  135.   if (Beveled)
  136.     pos += TSize(1,1);
  137.   for (int i = 0; i < MaxDigits; i++, pos -= TSize(DigitW,0))
  138.     Led[i] = new TLedDigit(this, pos);
  139.  
  140.   // Modify the background color attribute to 3dFace (grey usually)
  141.   //
  142.   SetBkgndColor(TColor::Sys3dFace);
  143. }
  144.  
  145. //
  146. //
  147. //
  148. TLedWindow::~TLedWindow()
  149. {
  150.   for (int i = 0; i < 11; i++)
  151.     delete LedBmp[i];
  152. }
  153.  
  154. //
  155. //
  156. //
  157. void TLedWindow::SetupWindow()
  158. {
  159.   TWindow::SetupWindow();
  160.   UpdateLeds();          // Set up initial Led values
  161. }
  162.  
  163. //
  164. // DisplayNumber - Accept a number to Display
  165. //
  166. void TLedWindow::DisplayNumber(long value)
  167. {
  168.   LedValue = value;
  169.   if (ClockWindow)
  170.   {
  171.     ClockWork = LedValue;               // Work field for localtime
  172.     if (ClockWork < 86400l)             // Handle 0th Jan 1970
  173.       ClockWork = ClockWork + 18000l;   // Add 5 hours
  174.     tmLedValue = localtime(&ClockWork); // Point struct at Value
  175.   }
  176.   UpdateLeds();
  177. }
  178.  
  179. //
  180. // StartTimer - Used to Start an Led Timer Window
  181. //
  182. bool TLedWindow::StartTimer(int interval)
  183. {
  184.   // Set Timer (pass interval to SetTimer in milliseconds)
  185.   //
  186.   if (interval == 0 || interval > 60 || !SetTimer(1, interval*1000, 0))
  187.     return false;
  188.  
  189.   TimerActive = true;
  190.   TimerInterval = interval;
  191.   return true;
  192. }
  193.  
  194. //
  195. // StopTimer - Stops a previously started Led timer window
  196. //
  197. void TLedWindow::StopTimer()
  198. {
  199.   if (TimerActive)
  200.   {
  201.     KillTimer(1);
  202.     TimerActive = false;
  203.     TimerInterval = 0;
  204.   }
  205. }
  206.  
  207. //
  208. // Called for each WM_TIMER received by this TLedWindow
  209. //
  210. void TLedWindow::EvTimer(UINT)
  211. {
  212.   LedValue = LedValue + 1;
  213.   if (ClockWindow)
  214.   {
  215.     ClockWork = ClockWork + 1;                // Work field for localtime
  216.     tmLedValue = localtime(&ClockWork);       // Point struct at Value
  217.   }
  218.   UpdateLeds();
  219. }
  220.  
  221. //
  222. // Paint TLedWindow bevel if enabled
  223. //
  224. void TLedWindow::Paint(TDC& dc, bool /*erase*/, TRect& /*rect*/)
  225. {
  226.   if (Beveled) {
  227.     dc.SelectObject(TPen(TColor::Sys3dFace));
  228.     dc.MoveTo(0, 0);
  229.     dc.LineTo(MaxDigits*DigitW+1, 0);
  230.     dc.MoveTo(0, 0);
  231.     dc.LineTo(0, DigitH+1);
  232.  
  233.     dc.SelectObject(TPen(TColor::Sys3dHilight));
  234.     dc.MoveTo(0, DigitH+1);
  235.     dc.LineTo(MaxDigits*DigitW+1, DigitH+1);
  236.     dc.MoveTo(MaxDigits*DigitW+1, 0);
  237.     dc.LineTo(MaxDigits*DigitW+1, DigitH+2);
  238.  
  239.     dc.SetPixel(TPoint(MaxDigits*DigitW+1, 0), TColor(192, 192, 192));
  240.     dc.SetPixel(TPoint(0, DigitH+1), TColor(192, 192, 192));
  241.   }
  242. }
  243.  
  244. //
  245. // UpdateLeds - Work out which LedDigits need to be replaced,
  246. // then invalidate the little suckers. This is simple except if
  247. // Clock = true, in which case we need to handle ':'s.
  248. //
  249. void TLedWindow::UpdateLeds()
  250. {
  251.   long value = LedValue;
  252.   for (int i = 0; i < MaxDigits; i++) {
  253.     int  digit;
  254.     if (ClockWindow && (i == 2 || i == 5))
  255.       // Digit 10 is ':', use for positions 2 and 5 of clocks
  256.       digit = 10;
  257.     else {
  258.       if (ClockWindow && tmLedValue) {
  259.         // Treat Clock as 3 different values
  260.         if (i == 0)
  261.           value = tmLedValue->tm_sec;
  262.         else if (i == 3)
  263.           value = tmLedValue->tm_min;
  264.         else if (i == 6)
  265.           value = tmLedValue->tm_hour;
  266.       }
  267.       digit = (int)(value - (value/10)*10);
  268.       value = value/10;
  269.     }
  270.     Led[i]->UpdateDigit(LedBmp[digit]);
  271.   }
  272. }
  273.